/**********************************************************************
* This file is part of project adempiere-xmlrpc                       *
* Copyright (C) Fabio Canella - Binario01                             *
* Copyright (C) Contributors                                          *
*                                                                     *
* This program is free software; you can redistribute it and/or       *
* modify it under the terms of the GNU General Public License         *
* as published by the Free Software Foundation; either version 2      *
* of the License, or (at your option) any later version.              *
*                                                                     *
* This program is distributed in the hope that it will be useful,     *
* but WITHOUT ANY WARRANTY; without even the implied warranty of      *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        *
* GNU General Public License for more details.                        *
*                                                                     *
* You should have received a copy of the GNU General Public License   *
* along with this program; if not, write to the Free Software         *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,          *
* MA 02110-1301, USA.                                                 *
*                                                                     *   
* *********************************************************************
* */

package it.binario01.rpc;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;

import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.model.MProduct;
import org.compiere.util.CPreparedStatement;
import org.compiere.util.DB;
import org.compiere.util.Env;


public class Handler {
	
	final static int ad_client_id 	= 1000005;
	final static int ad_org_id 		= 1000012;
	
	
   public Handler() {
   }
   
   public String inBound(String from, String to) {
	   System.out.println("metodo invocato");
      return "Thank you very very much, " + from + ", for calling " + to;
   }
   
   public String inBound2(String from, String to) {
	   System.out.println("metodo invocato");
      return "Thank you, " + from + ", for calling " + to;
   }
   
   public String inBound3(String from, String to) {
	   System.out.println("metodo invocato");
      return "Thank you, " + from + ", for calling " + to;
   }
   
   
   public int setStrValue(String tableName, int id, String columnName, String value) {
	   String query = "update " + tableName + " set " + columnName +
			" = '" + value + "' where " + tableName + "_id = " + id;
	  
	   int retVal = DB.executeUpdate(query);
	   System.out.println("DEBUG " + query);
	   if (retVal < 0)
		   System.out.println(query);
	   return retVal;
   }//setStrValue
   
   
   public int setIntValue(String tableName, int id, String columnName, int value) {
	   String query = "update " + tableName + " set " + columnName +
		" = " + value + " where " + tableName + "_id = " + id;
 
	  int retVal = DB.executeUpdate(query);
	  System.out.println("DEBUG " + query);
	  if (retVal < 0)
		   System.out.println(query);
	  return retVal;
   }//setIntValue
   
   
   
   public int setIntValue(String tableName, int id, String columnName, BigDecimal value) {
	   int retValue = -1;
	   String query = "update " + tableName + " set " + columnName +
			" = ? where " + tableName + "_id = " + id;
	   try {
		   CPreparedStatement ps = DB.prepareStatement(query);
		   ps.setBigDecimal(0, value);
		   retValue = ps.executeUpdate();
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   System.out.println("DEBUG " + query);
	   if (retValue < 0)
		   System.out.println(query);
	   return retValue;

   }
   
   public int setDateValue(String tableName, int id, String columnName, Date value) {
	   int retValue = -1;
	   String query = "update " + tableName + " set " + columnName +
			" = ? where " + tableName + "_id = " + id;
	   try {
		   CPreparedStatement ps = DB.prepareStatement(query);
		   ps.setDate(0, value);
		   return ps.executeUpdate();
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   if (retValue < 0)
		   System.out.println(query);
	   return retValue;
   }
   
   
   
   public int setDateStrValue(String tableName, int id, String columnName, String value) {
	   
	   int retValue = -1;
	   
	   try {
		   
		   System.out.println("Data da parsare " + value);

		   DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		   java.util.Date date = (java.util.Date)formatter.parse(value);
	
		   String query = "update " + tableName + " set " + columnName +
				" = ? where " + tableName + "_id = " + id;
		   try {
		       PreparedStatement pstmt = DB.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, null);
		       pstmt.setDate(1, new java.sql.Date(date.getTime()));
		       return pstmt.executeUpdate();
		   } catch (Exception e) {
			   e.printStackTrace();
		   }
		   
		   System.out.println("DEBUG " + query);
		   
		   if (retValue < 0)
			   System.out.println(query);
	   
	   } catch (Exception e) {
		   e.printStackTrace();
	   }
	   
	   return retValue;
   }
 
   
   
   public int createOrder(String documentno, String docstatus, 
		   int c_doctype_id, int c_doctypetarget_id, java.util.Date dateordered,
		   java.util.Date datepromised, String paymentrule,
		   int c_bpartner_id, int c_bpartner_location_id, int c_currency_id,
		   int m_warehouse_id, int m_pricelist_id, int c_paymentterm_id) {
	   int retValue = -1;
	   
	   try {
		   MOrder order = new MOrder (getProperties(), 0, null);
		   order.setC_BPartner_ID(c_bpartner_id);
		   if (c_bpartner_location_id > 0)
			   order.setC_BPartner_Location_ID(c_bpartner_location_id);
		   if (c_currency_id > 0)
			   order.setC_Currency_ID(c_currency_id);
		   if (m_warehouse_id > 0)
			   order.setM_Warehouse_ID(m_warehouse_id);
		   if (c_doctypetarget_id > 0)
			   order.setC_DocTypeTarget_ID(c_doctypetarget_id);
		   if (dateordered != null) 
			   order.setDateOrdered(new Timestamp(dateordered.getTime()));
		   if (datepromised != null)
			   order.setDatePromised(new Timestamp(datepromised.getTime()));
		   if (paymentrule != null)
			   order.setPaymentRule(paymentrule);
		   if (m_pricelist_id > 0)
			   order.setM_PriceList_ID(m_pricelist_id);
		   if (c_paymentterm_id > 0)
			   order.setC_PaymentTerm_ID(c_paymentterm_id);
		   
		   order.setC_DocType_ID(c_doctype_id);
		   order.setDocStatus(docstatus);
		   
		   System.out.println("creazione Ordini");
		   
		   if (order.save()) {
			   return order.get_ID();
		   } else {
			   System.out.println("errore nel salvataggio dell'ordine");
			   return -1;
		   }
	   } catch (Exception e) {
		   System.out.println(e.getMessage());
		   e.printStackTrace();
	   }

	   return retValue;
		   
   }
   
   
   public int createOrderLine(int c_order_id, int m_product_id, int c_uom_id) {
	   int retValue = -1;
	   
	   BigDecimal qta 			= Env.ZERO;
	   BigDecimal pricelist 	= Env.ZERO;
	   BigDecimal priceactual 	= Env.ZERO;
	   
	   try {
		   MOrder		order		= new MOrder(getProperties(), c_order_id, null);
		   MOrderLine 	orderline 	= new MOrderLine (order);
		   
		   orderline.setProduct(new MProduct(getProperties(), m_product_id, null));
		   orderline.setQty(qta);
		   orderline.setC_UOM_ID(c_uom_id);
		   orderline.setPrice();
		   
		   
		   System.out.println("creazione OrderLine");
		   
		   if (orderline.save()) {
			   return orderline.get_ID();
		   } else {
			   System.out.println("errore nel salvataggio dell'orderline");
			   return -1;
		   }
	   } catch (Exception e) {
		   System.out.println(e.getMessage());
		   e.printStackTrace();
	   }
	   return retValue;
   }
   

   public int createOrderLine(int c_order_id, int m_product_id, int c_uom_id, String strQta, 
		   String strPricelist, String strPriceactual, int c_tax_id) {
	   int retValue = -1;
	   
	   try {
		   MOrder		order		= new MOrder(getProperties(), c_order_id, null);
		   MOrderLine 	orderline 	= new MOrderLine (order);
		   
		   orderline.setProduct(new MProduct(getProperties(), m_product_id, null));
		   orderline.setQty(new BigDecimal(strQta));
		   orderline.setC_UOM_ID(c_uom_id);
		   orderline.setC_Tax_ID(c_tax_id);
		   orderline.setPriceList(new BigDecimal(strPricelist));
		   orderline.setPriceActual(new BigDecimal(strPriceactual));
		   orderline.setPrice();
		   
		   System.out.println("creazione OrderLine");
		   
		   if (orderline.save()) {
			   return orderline.get_ID();
		   } else {
			   System.out.println("errore nel salvataggio dell'orderline");
			   return -1;
		   }
	   } catch (Exception e) {
		   System.out.println(e.getMessage());
		   e.printStackTrace();
	   }

	   return retValue;
		   
   }
   
   private Properties getProperties() {
	   
	   Properties ctx = new Properties();
	   Env.setContext(ctx, "#AD_Client_ID", ad_client_id);
	   Env.setContext(ctx, "#AD_Org_ID", 	ad_org_id);
	   
	   return ctx;
	   
   }
   
   
}
